package com.softer.chengxin.login.controller;


import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.common.util.DateFormatUtil;
import com.softer.chengxin.corp.domin.Corp;
import com.softer.chengxin.login.service.LoginService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author Gong Zhiyang
 * @date 2018/9/26
 */
@RestController
public class LoginController {
    @Autowired
    private LoginService loginService;

    /**
     * 登录
     *
     * @param UserName  登陆名
     * @param PassWord  登陆密码
     * @param LoginType 登陆类型（0主管部门，1物业企业）
     * @param KeyDog    加密狗
     * @return 登陆结果
     */
    @PostMapping("/api/UserInfo/Login")
    public CommonResult getUserByUserNameAndPassword(String UserName, String PassWord, String LoginType, String KeyDog, HttpSession session) {
        CommonResult map = loginService.getUserByUserNameAndPassword(UserName, PassWord, LoginType, KeyDog, session);
        return map;
    }

    /**
     * 注册
     * 2018-07-31T16:00:00.000Z
     */
    @PostMapping("/api/WY_CorpBaseInfo/Register")
    public CommonResult CorpBaseInfoRegister(Corp corp) {
        try {
            if (StringUtils.isNotBlank(corp.getBusinessTime())) {
                String time = DateFormatUtil.transferTime(corp.getBusinessTime());
                corp.setBusinessTime(time);
            }
        } catch (Exception e) {
            e.getStackTrace();
        }
        return loginService.addCorp(corp);
    }

    /**
     * 退出登录
     */
    @RequestMapping("/api/UserInfo/Logout")
    public CommonResult Logout(HttpServletRequest request) {

        //防止创建Session
        HttpSession session = request.getSession(false);
        return loginService.loginOut(session);
    }
}
